
using System;
using System.Reflection;
using System.Collections;
using UniversalStorageServices;
using UniversalStorageServices.PersistenceManager;

		

namespace Example
{
		
	public interface IPerson
	{
		string Id { get; set; }
		System.String Name { get; set; }
		System.String Firstname { get; set; }
		System.DateTime Birthday { get; set; }
		
		IJob Job { get; }
		
	}
	
	
		/// <summary>
	/// <para>
	/// A collection that stores <see cref='Person'/> objects.
	/// </para>
	/// </summary>
	/// <seealso cref='PersonCollection'/>
	[Serializable()]
	public class  PersonCollection : IPersistableCollection 
	{	
		/// <summary>
		/// <para>
		/// Initializes a new instance of <see cref='PersonCollection'/>.
		/// </para>
		/// </summary>
		public PersonCollection()
		{
		}
		
		/// <summary>
		/// <para>
		/// Initializes a new instance of <see cref='PersonCollection'/> based on another <see cref='PersonCollection'/>.
		/// </para>
		/// </summary>
		/// <param name='val'>
		/// A <see cref='PersonCollection'/> from which the contents are copied
		/// </param>
		public PersonCollection( PersonCollection val)
		{
			this.AddRange(val);	
		}
		
		/// <summary>
		/// <para>
		/// Initializes a new instance of <see cref='PersonCollection'/> containing any array of <see cref='Person'/> objects.
		/// </para>
		/// </summary>
		/// <param name='val'>
		/// A array of <see cref='Person'/> objects with which to intialize the collection
		/// </param>
		public PersonCollection( Person[] val)
		{
			this.AddRange(val);
		}
		
		public PersonCollection (Entity parentEntity, string role, IPersistableCollection data): base(parentEntity, role, data)
		{
		}
		
		/// <summary>
		/// <para>Represents the entry at the specified index of the <see cref='Person'/>.</para>
		/// </summary>
		/// <param name='index'><para>The zero-based index of the entry to locate in the collection.</para></param>
		/// <value>
		/// <para> The entry at the specified index of the collection.</para>
		/// </value>
		/// <exception cref='System.ArgumentOutOfRangeException'><paramref name='index'/> is outside the valid range of indexes for the collection.</exception>
		public new Person this[int index] 
		{
			get 
			{
				return ((Person)base[index]);
			}
			set 
			{
				base[index] = value;
			}
		}
		
		/// <summary>
		/// <para>Adds a <see cref='Person'/> with the specified value to the 
		/// <see cref='PersonCollection'/> .</para>
		/// </summary>
		/// <param name='val'>The <see cref='Person'/> to add.</param>
		/// <returns>
		/// <para>The index at which the new element was inserted.</para>
		/// </returns>
		/// <seealso cref='PersonCollection.AddRange'/>
		public int Add( Person val)
		{
			return base.Add(val);
		}
		
	
		
		/// <summary>
		/// <para>
		/// Adds the contents of another <see cref='PersonCollection'/> to the end of the collection.
		/// </para>
		/// </summary>
		/// <param name='val'>
		/// A <see cref='PersonCollection'/> containing the objects to add to the collection.
		/// </param>
		/// <returns>
		/// <para>None.</para>
		/// </returns>
		/// <seealso cref='PersonCollection.Add'/>
		public void AddRange( PersonCollection val)
		{
			base.AddRange(val);
		}
		public void AddRange( Person[] val)
		{
			base.AddRange(val);
		}
		
		/// <summary>
		/// <para>Gets a value indicating whether the 
		/// <see cref='PersonCollection'/> contains the specified <see cref='Person'/>.</para>
		/// </summary>
		/// <param name='val'>The <see cref='Person'/> to locate.</param>
		/// <returns>
		/// <para><see langword='true'/> if the <see cref='Person'/> is contained in the collection; 
		/// otherwise, <see langword='false'/>.</para>
		/// </returns>
		/// <seealso cref='PersonCollection.IndexOf'/>
		public bool Contains( Person val)
		{
			return List.Contains(val);
		}
		
		/// <summary>
		/// <para>Copies the <see cref='PersonCollection'/> values to a one-dimensional <see cref='System.Array'/> instance at the 
		/// specified index.</para>
		/// </summary>
		/// <param name='array'><para>The one-dimensional <see cref='System.Array'/> that is the destination of the values copied from <see cref='PersonCollection'/> .</para></param>
		/// <param name='index'>The index in <paramref name='array'/> where copying begins.</param>
		/// <returns>
		/// <para>None.</para>
		/// </returns>
		/// <exception cref='System.ArgumentException'><para><paramref name='array'/> is multidimensional.</para> <para>-or-</para> <para>The number of elements in the <see cref='PersonCollection'/> is greater than the available space between <paramref name='index'/> and the end of <paramref name='array'/>.</para></exception>
		/// <exception cref='System.ArgumentNullException'><paramref name='array'/> is <see langword='null'/>. </exception>
		/// <exception cref='System.ArgumentOutOfRangeException'><paramref name='index'/> is less than <paramref name='array'/>'s lowbound. </exception>
		/// <seealso cref='System.Array'/>
		public void CopyTo( Person[] array, int index)
		{
			List.CopyTo(array, index);
		}
		
		/// <summary>
		/// <para>Returns the index of a <see cref='Person'/> in 
		/// the <see cref='PersonCollection'/> .</para>
		/// </summary>
		/// <param name='val'>The <see cref='Person'/> to locate.</param>
		/// <returns>
		/// <para>The index of the <see cref='Person'/> of <paramref name='val'/> in the 
		/// <see cref='PersonCollection'/>, if found; otherwise, -1.</para>
		/// </returns>
		/// <seealso cref='PersonCollection.Contains'/>
		public int IndexOf( Person val)
		{
			return List.IndexOf(val);
		}
		
		/// <summary>
		/// <para>Inserts a <see cref='Person'/> into the <see cref='PersonCollection'/> at the specified index.</para>
		/// </summary>
		/// <param name='index'>The zero-based index where <paramref name='val'/> should be inserted.</param>
		/// <param name='val'>The <see cref='Person'/> to insert.</param>
		/// <returns><para>None.</para></returns>
		/// <seealso cref='PersonCollection.Add'/>
		public void Insert(int index,  Person val)
		{
			List.Insert(index, val);
		}
		
		/// <summary>
		/// <para>Returns an enumerator that can iterate through 
		/// the <see cref='PersonCollection'/> .</para>
		/// </summary>
		/// <returns><para>None.</para></returns>
		/// <seealso cref='System.Collections.IEnumerator'/>
		public new  PersonEnumerator GetEnumerator()
		{
			return new  PersonEnumerator(this);
		}
		
		/// <summary>
		/// <para> Removes a specific <see cref='Person'/> from the 
		/// <see cref='PersonCollection'/> .</para>
		/// </summary>
		/// <param name='val'>The <see cref='Person'/> to remove from the <see cref='PersonCollection'/> .</param>
		/// <returns><para>None.</para></returns>
		/// <exception cref='System.ArgumentException'><paramref name='val'/> is not found in the Collection. </exception>
		public void Remove( Person val)
		{
			List.Remove(val);
		}
		
		public class  PersonEnumerator : IEnumerator
		{
			IEnumerator baseEnumerator;
			IEnumerable temp;
			
			public  PersonEnumerator( PersonCollection mappings)
			{
				this.temp = mappings;
				this.baseEnumerator = temp.GetEnumerator();
			}
			
			public  Person Current 
			{
				get 
				{
					return (( Person)(baseEnumerator.Current));
				}
			}
			
			object IEnumerator.Current 
			{
				get 
				{
					return baseEnumerator.Current;
				}
			}
			
			public bool MoveNext()
			{
				return baseEnumerator.MoveNext();
			}
			
			bool IEnumerator.MoveNext()
			{
				return baseEnumerator.MoveNext();
			}
			
			public void Reset()
			{
				baseEnumerator.Reset();
			}
			
			void IEnumerator.Reset()
			{
				baseEnumerator.Reset();
			}
		}
	}

	
	[Serializable]
	public class Person :  IPersistable, IPerson 
	{		
	
		protected Entity _Entity;
		
		[NonSerialized]
		protected PersistenceManager _PersistenceManager;
		
		Entity IPersistable.Entity
		{
			get
			{
				return _Entity;
			}
			set
			{
				_Entity = value;
			}
		}
		
		PersistenceManager IPersistable.PersistenceManager
		{
			get
			{
				return _PersistenceManager;
			}
			set
			{
				_PersistenceManager = value;
			}
		}
		
		public string Id
		{
			get
			{
				return _Entity.Id;
			}
			set
			{
				_Entity.Id = value;
			}
		}
		
		~Person()
		{
			if(_PersistenceManager != null)
				_PersistenceManager.RemoveFromCache(this);
		}
	
		void IPersistable.TrackChildren()
		{
			// if the object is not tracked there is nothing to do
			if(_PersistenceManager == null)
				return;
	
			if(_Job != null)
				_PersistenceManager.TrackObject(_Job);
		
		}
		
		void IPersistable.DeleteChildren()
		{	
	
		}
	
		public System.String Name
		{
			get 
			{ 
				if(_Entity.GetValue("Name") == null)
					return (new System.String[1])[0];
				else
					return (System.String)this._Entity["Name"]; 
			}
		
			set 
			{ 
				this._Entity["Name"] = value; 
			}
		}
		
		public System.String Firstname
		{
			get 
			{ 
				if(_Entity.GetValue("Firstname") == null)
					return (new System.String[1])[0];
				else
					return (System.String)this._Entity["Firstname"]; 
			}
		
			set 
			{ 
				this._Entity["Firstname"] = value; 
			}
		}
		
		public System.DateTime Birthday
		{
			get 
			{ 
				if(_Entity.GetValue("Birthday") == null)
					return (new System.DateTime[1])[0];
				else
					return (System.DateTime)this._Entity["Birthday"]; 
			}
		
			set 
			{ 
				this._Entity["Birthday"] = value; 
			}
		}
		
		private bool _IsJobLoaded = false;

		private Job _Job;

		public Job Job
		{
			get 
			{ 
				if(!_IsJobLoaded && _PersistenceManager != null)
				{
					IPersistableCollection items = _PersistenceManager.LoadReference(_Entity, "Job");
					if(items.Count > 0)
						_Job = (Job)items[0];
				}
				
				_IsJobLoaded = true;
				return _Job;
			}
			set
			{
				// Removes the reference to an existing entity
				if(Job != null)
				{
					_Entity.DeleteElement("Job");
				}
				
				// Creates the reference only if it is a concrete entity
				if(value != null)
				{
					_Entity.AddValue("Job", ((IPersistable)value).Entity);
				}
				_Job = value;
			}
		}				
			
		IJob IPerson.Job
		{
			get { return Job; }
		}
		
		public Person ()
		{
			
			this._Entity = new Entity(PersistenceManager.ConvertNamespaceDomainToEuss(this.GetType().FullName));
		}

		public Person (Entity e): this()
		{
			this._Entity = e;
		}

		private ParentRelationships _Parents;

		public ParentRelationships Parents
		{
			get
			{
				if (_Parents == null)
					if (_PersistenceManager == null)
						throw new ApplicationException("Can't call Parents untracked objects");
				else
					_Parents = new ParentRelationships(this);

				return _Parents;
			}
		}

		public class ParentRelationships
		{
			private IPersistable _Owner;
	
			public ParentRelationships(IPersistable o)
			{
				_Owner = o;
			}
		
		}
	}
	
	public interface IJob
	{
		string Id { get; set; }
		System.String Title { get; set; }
		System.DateTime StartDate { get; set; }
		
	}
	
	
		/// <summary>
	/// <para>
	/// A collection that stores <see cref='Job'/> objects.
	/// </para>
	/// </summary>
	/// <seealso cref='JobCollection'/>
	[Serializable()]
	public class  JobCollection : IPersistableCollection 
	{	
		/// <summary>
		/// <para>
		/// Initializes a new instance of <see cref='JobCollection'/>.
		/// </para>
		/// </summary>
		public JobCollection()
		{
		}
		
		/// <summary>
		/// <para>
		/// Initializes a new instance of <see cref='JobCollection'/> based on another <see cref='JobCollection'/>.
		/// </para>
		/// </summary>
		/// <param name='val'>
		/// A <see cref='JobCollection'/> from which the contents are copied
		/// </param>
		public JobCollection( JobCollection val)
		{
			this.AddRange(val);	
		}
		
		/// <summary>
		/// <para>
		/// Initializes a new instance of <see cref='JobCollection'/> containing any array of <see cref='Job'/> objects.
		/// </para>
		/// </summary>
		/// <param name='val'>
		/// A array of <see cref='Job'/> objects with which to intialize the collection
		/// </param>
		public JobCollection( Job[] val)
		{
			this.AddRange(val);
		}
		
		public JobCollection (Entity parentEntity, string role, IPersistableCollection data): base(parentEntity, role, data)
		{
		}
		
		/// <summary>
		/// <para>Represents the entry at the specified index of the <see cref='Job'/>.</para>
		/// </summary>
		/// <param name='index'><para>The zero-based index of the entry to locate in the collection.</para></param>
		/// <value>
		/// <para> The entry at the specified index of the collection.</para>
		/// </value>
		/// <exception cref='System.ArgumentOutOfRangeException'><paramref name='index'/> is outside the valid range of indexes for the collection.</exception>
		public new Job this[int index] 
		{
			get 
			{
				return ((Job)base[index]);
			}
			set 
			{
				base[index] = value;
			}
		}
		
		/// <summary>
		/// <para>Adds a <see cref='Job'/> with the specified value to the 
		/// <see cref='JobCollection'/> .</para>
		/// </summary>
		/// <param name='val'>The <see cref='Job'/> to add.</param>
		/// <returns>
		/// <para>The index at which the new element was inserted.</para>
		/// </returns>
		/// <seealso cref='JobCollection.AddRange'/>
		public int Add( Job val)
		{
			return base.Add(val);
		}
		
	
		
		/// <summary>
		/// <para>
		/// Adds the contents of another <see cref='JobCollection'/> to the end of the collection.
		/// </para>
		/// </summary>
		/// <param name='val'>
		/// A <see cref='JobCollection'/> containing the objects to add to the collection.
		/// </param>
		/// <returns>
		/// <para>None.</para>
		/// </returns>
		/// <seealso cref='JobCollection.Add'/>
		public void AddRange( JobCollection val)
		{
			base.AddRange(val);
		}
		public void AddRange( Job[] val)
		{
			base.AddRange(val);
		}
		
		/// <summary>
		/// <para>Gets a value indicating whether the 
		/// <see cref='JobCollection'/> contains the specified <see cref='Job'/>.</para>
		/// </summary>
		/// <param name='val'>The <see cref='Job'/> to locate.</param>
		/// <returns>
		/// <para><see langword='true'/> if the <see cref='Job'/> is contained in the collection; 
		/// otherwise, <see langword='false'/>.</para>
		/// </returns>
		/// <seealso cref='JobCollection.IndexOf'/>
		public bool Contains( Job val)
		{
			return List.Contains(val);
		}
		
		/// <summary>
		/// <para>Copies the <see cref='JobCollection'/> values to a one-dimensional <see cref='System.Array'/> instance at the 
		/// specified index.</para>
		/// </summary>
		/// <param name='array'><para>The one-dimensional <see cref='System.Array'/> that is the destination of the values copied from <see cref='JobCollection'/> .</para></param>
		/// <param name='index'>The index in <paramref name='array'/> where copying begins.</param>
		/// <returns>
		/// <para>None.</para>
		/// </returns>
		/// <exception cref='System.ArgumentException'><para><paramref name='array'/> is multidimensional.</para> <para>-or-</para> <para>The number of elements in the <see cref='JobCollection'/> is greater than the available space between <paramref name='index'/> and the end of <paramref name='array'/>.</para></exception>
		/// <exception cref='System.ArgumentNullException'><paramref name='array'/> is <see langword='null'/>. </exception>
		/// <exception cref='System.ArgumentOutOfRangeException'><paramref name='index'/> is less than <paramref name='array'/>'s lowbound. </exception>
		/// <seealso cref='System.Array'/>
		public void CopyTo( Job[] array, int index)
		{
			List.CopyTo(array, index);
		}
		
		/// <summary>
		/// <para>Returns the index of a <see cref='Job'/> in 
		/// the <see cref='JobCollection'/> .</para>
		/// </summary>
		/// <param name='val'>The <see cref='Job'/> to locate.</param>
		/// <returns>
		/// <para>The index of the <see cref='Job'/> of <paramref name='val'/> in the 
		/// <see cref='JobCollection'/>, if found; otherwise, -1.</para>
		/// </returns>
		/// <seealso cref='JobCollection.Contains'/>
		public int IndexOf( Job val)
		{
			return List.IndexOf(val);
		}
		
		/// <summary>
		/// <para>Inserts a <see cref='Job'/> into the <see cref='JobCollection'/> at the specified index.</para>
		/// </summary>
		/// <param name='index'>The zero-based index where <paramref name='val'/> should be inserted.</param>
		/// <param name='val'>The <see cref='Job'/> to insert.</param>
		/// <returns><para>None.</para></returns>
		/// <seealso cref='JobCollection.Add'/>
		public void Insert(int index,  Job val)
		{
			List.Insert(index, val);
		}
		
		/// <summary>
		/// <para>Returns an enumerator that can iterate through 
		/// the <see cref='JobCollection'/> .</para>
		/// </summary>
		/// <returns><para>None.</para></returns>
		/// <seealso cref='System.Collections.IEnumerator'/>
		public new  JobEnumerator GetEnumerator()
		{
			return new  JobEnumerator(this);
		}
		
		/// <summary>
		/// <para> Removes a specific <see cref='Job'/> from the 
		/// <see cref='JobCollection'/> .</para>
		/// </summary>
		/// <param name='val'>The <see cref='Job'/> to remove from the <see cref='JobCollection'/> .</param>
		/// <returns><para>None.</para></returns>
		/// <exception cref='System.ArgumentException'><paramref name='val'/> is not found in the Collection. </exception>
		public void Remove( Job val)
		{
			List.Remove(val);
		}
		
		public class  JobEnumerator : IEnumerator
		{
			IEnumerator baseEnumerator;
			IEnumerable temp;
			
			public  JobEnumerator( JobCollection mappings)
			{
				this.temp = mappings;
				this.baseEnumerator = temp.GetEnumerator();
			}
			
			public  Job Current 
			{
				get 
				{
					return (( Job)(baseEnumerator.Current));
				}
			}
			
			object IEnumerator.Current 
			{
				get 
				{
					return baseEnumerator.Current;
				}
			}
			
			public bool MoveNext()
			{
				return baseEnumerator.MoveNext();
			}
			
			bool IEnumerator.MoveNext()
			{
				return baseEnumerator.MoveNext();
			}
			
			public void Reset()
			{
				baseEnumerator.Reset();
			}
			
			void IEnumerator.Reset()
			{
				baseEnumerator.Reset();
			}
		}
	}

	
	[Serializable]
	public class Job :  IPersistable, IJob 
	{		
	
		protected Entity _Entity;
		
		[NonSerialized]
		protected PersistenceManager _PersistenceManager;
		
		Entity IPersistable.Entity
		{
			get
			{
				return _Entity;
			}
			set
			{
				_Entity = value;
			}
		}
		
		PersistenceManager IPersistable.PersistenceManager
		{
			get
			{
				return _PersistenceManager;
			}
			set
			{
				_PersistenceManager = value;
			}
		}
		
		public string Id
		{
			get
			{
				return _Entity.Id;
			}
			set
			{
				_Entity.Id = value;
			}
		}
		
		~Job()
		{
			if(_PersistenceManager != null)
				_PersistenceManager.RemoveFromCache(this);
		}
	
		void IPersistable.TrackChildren()
		{
			// if the object is not tracked there is nothing to do
			if(_PersistenceManager == null)
				return;
	
		}
		
		void IPersistable.DeleteChildren()
		{	
	
		}
	
		public System.String Title
		{
			get 
			{ 
				if(_Entity.GetValue("Title") == null)
					return (new System.String[1])[0];
				else
					return (System.String)this._Entity["Title"]; 
			}
		
			set 
			{ 
				this._Entity["Title"] = value; 
			}
		}
		
		public System.DateTime StartDate
		{
			get 
			{ 
				if(_Entity.GetValue("StartDate") == null)
					return (new System.DateTime[1])[0];
				else
					return (System.DateTime)this._Entity["StartDate"]; 
			}
		
			set 
			{ 
				this._Entity["StartDate"] = value; 
			}
		}
		
		public Job ()
		{
			
			this._Entity = new Entity(PersistenceManager.ConvertNamespaceDomainToEuss(this.GetType().FullName));
		}

		public Job (Entity e): this()
		{
			this._Entity = e;
		}

		private ParentRelationships _Parents;

		public ParentRelationships Parents
		{
			get
			{
				if (_Parents == null)
					if (_PersistenceManager == null)
						throw new ApplicationException("Can't call Parents untracked objects");
				else
					_Parents = new ParentRelationships(this);

				return _Parents;
			}
		}

		public class ParentRelationships
		{
			private IPersistable _Owner;
	
			public ParentRelationships(IPersistable o)
			{
				_Owner = o;
			}
		
			public Person GetPerson()
			{
				IPersistableCollection items = _Owner.PersistenceManager.Load(typeof(Person), String.Format("[count(Job[Id = '{0}']) > 0]", _Owner.Id));
			
				if (items.Count > 0)
					return (Person)items[0];
				else
					return null;
			}
	
		}
	}

}
